package com.link.base.base.annualplan.controller;

import com.link.base.base.annualplan.model.AnnualPlan;
import com.link.base.base.annualplan.service.AnnualPlanService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liushoulong
 * @date 2018-11-08
 * @desc 年度规划
 */
@Controller("AnnualPlanController")
@RequestMapping("/link/annualPlan")
public class AnnualPlanController extends BasicController<AnnualPlan> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private AnnualPlanService annualPlanService;



    @Override
    public BasicService<AnnualPlan> getBasicService() throws Exception {
        return annualPlanService;
    }


    /**
     * 新建一条记录
     * 新建所有任务分解行记录
     */
    @RequestMapping(value = "/annualPlanInsert")
    @ResponseBody
    public Map<String, Object> annualPlanInsert(@JsonParam AnnualPlan annualPlan, HttpSession session,
                                                HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(8);
        try {
            annualPlanService.annualPlanInsert(annualPlan);
            AnnualPlan newEntry = getBasicService().queryById(annualPlan);
            result.put("newRow", newEntry);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @Override
    @RequestMapping(value = "/deleteById")
    @ResponseBody
    public Map<String, Object> deleteById(@JsonParam AnnualPlan entity, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> result = new HashMap<String, Object>(8);
        try {
            annualPlanService.deleteCascadePlan(entity);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
